import Vue from "vue";

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

//设置cookie

function SetTheCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
   /* console.info(cname + "=" + encodeURI(cvalue) + "; " + expires);*/
    document.cookie = cname + "=" + encodeURI(cvalue) + ";path=/;" + expires;
   /* console.info(document.cookie);*/
};
//获取cookie
 function GetTheCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) return decodeURI(c.substring(name.length, c.length));
        }
        return "";
    };

//清除cookie
function ClearTheCookie(u) {
    SetTheCookie(u, "", -1);

};

//用于生成uuid
function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
//shareUrlReport 上传用户行为调用的方法
//reportbehaviourData:用户行为behaviours字段,json格式
window.shareUrlReport = function (reportbehaviourData, actiontype) {
    var userAgent = navigator.userAgent;
    if (actiontype != "comment") {//评论用户行为没有shareType
        if (userAgent.match(/MicroMessenger/i)) {
            reportbehaviourData.sharetype = "weixin";
        } else if (userAgent.match(/QQ\//i) == 'QQ/') {
            reportbehaviourData.sharetype = "cqq";
        } else if (userAgent.match(/WeiBo/i)) {
            reportbehaviourData.sharetype = "sina";
        } else {
            reportbehaviourData.sharetype = "web browser";
        }
    }
    reportbehaviourData.ip = userip;

    var imeicookie=GetTheCookie("YF_XMT_IMEI_COOKIE");//从cookie获取虚拟imei

    if(imeicookie==""||typeof (imeicookie)=="undefined"){
        imeicookie=guid();//如果没有则生成一个
        SetTheCookie("YF_XMT_IMEI_COOKIE",imeicookie,60);//存入cookie中设置较长失效期
    }else {//如果有则延长时间
        ClearTheCookie("YF_XMT_IMEI_COOKIE");
        SetTheCookie("YF_XMT_IMEI_COOKIE",imeicookie,60);
    }
    reportbehaviourData.imei=imeicookie;//imei使用该数据
// 配置参考文档中1.5.1输入参数中的actioncontext参数
    var actioncontext = {
        version: "1.1",// 此处版本号为1.1
        data: reportbehaviourData,

    };
    var infobehaviours = {
        actiontype: actiontype,
        createtime: new Date().Format("yyyyMMddhhmmss"),
        actioncontext: actioncontext
    };
    if (userAgent.match(/Android/ig)) {
        infobehaviours.client = "android";
    } else if (userAgent.match(/iPhone/ig) || userAgent.match(/iPad/ig)) {
        infobehaviours.client = "ios";
    } else {
        infobehaviours.client = "h5";
    }
    var behaviours = [infobehaviours];
    var reportdata = {
        sourcetype: "h5",
        behaviours: behaviours
    };
    // var apiUrl = "https://v.butel.com/webapi/userbehaviour/report";
    var apiUrl = countinterUrl + "webapi/userbehaviour/report";
    var postdata = JSON.stringify(reportdata);
//console.info(postdata);
    $.ajax({
        url: apiUrl,
        type: 'POST',
        data: postdata,
        dataType: 'json',
        contentType: 'application/json',
        error: function (data) {
            console.error("汇报失败:" + actiontype);
        },
        success: function (data) {
            console.log("汇报成功:" + actiontype);
        }
    });
};